#-*-coding:utf-8-*-

import pandas as pd
import requests
from bs4 import BeautifulSoup as bs
from prettytable import PrettyTable
from colorama import Fore, Back, Style  # һ��pythonר�������ڿ���̨�������������ɫ���ֵ�ģ��,Fore�����������ɫ��Back��������屳����ɫ��Style����������ʽ
from locale import *  # ��ǧλ�ָ���������ת��Ϊ������ֵ
# import csv
import xlwt
import itertools
import csv
import codecs
import json
# from compiler.ast import flatten
setlocale(LC_NUMERIC, 'English_US')

data0 = {'curr_id':'941155',
        'smlID':'1507702',
        'header':'BABA��ʷ����',
        'st_date':'2010/10/01',
        'end_date':'2019/11/30',
        'interval_sec':'Daily',
        'sort_col':'date',
        'sort_ord':'DESC',
        'action':'historical_data'}
header_selfdefine = {
    'Host': 'cn.investing.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0',
    'Accept':'text/plain, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Accept-Encoding': 'gzip, deflate, br',
    'Content-Type': 'application/x-www-form-urlencoded',
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Length': '193',
    'Origin': 'https://cn.investing.com',
    'Connection': 'keep-alive',
    'Referer': 'https://cn.investing.com/equities/alibaba-historical-data',
    'DNT': '1'
}
url = 'https://cn.investing.com/instruments/HistoricalDataAjax'

web_data = requests.post(url, data0, headers=header_selfdefine)
print (web_data.text)

soup = bs(web_data.text, 'lxml')

sumdata = soup.select('#results_box > table > tbody')  # �õ��б�
pt = [['����', '����', '����', '��', '��', '������']]
for onedata in sumdata[0].find_all('tr'):
    data = onedata.find_all('td')
    Date = data[0].get_text() # ����
    Close = data[1].get_text() # ����
    Open = data[2].get_text() #����
    High = data[3].get_text() # ��߼�
    Low = data[4].get_text() # ��ͼ�
    TovalVol = data[5].get_text()  # ������
    pt.append([Date, Close, Open, High, Low, TovalVol])
    # pt.add_row([Name, NewPrice, HighPrice, LowPrice, Change, Chg, TovalVol, Time])
print('������������ӢΪ���飺(https://cn.investing.com/equities/)')
print(pt)
kk = list(itertools.chain(*pt))
file_name = 'D://����Ͱ�2.csv'
file_csv = codecs.open(file_name, 'w+', 'utf-8-sig')  # ׷��
    # writer = csv.writer(file_csv)
writer = csv.writer(file_csv)
for d in range(len(kk)):
    # print(d)
    if d >= len(kk) / 6:
        break
    else:
        writer.writerow(
            [kk[d * 6], kk[d * 6 + 1], kk[d * 6 + 2], kk[d * 6 + 3], kk[d * 6 + 4], kk[d * 6 + 5]])

print("�����ļ��ɹ����������")